﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2012/2/8 19:18:04
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Restaurant.EntityType;
using Sector7.Framework.Core;
using System.Data.SqlClient;
using Sector7.Framework.Utility;
using Sector7.Restaurant.DAO.Utility;
using Sector7.Framework.DB;

namespace Sector7.Restaurant.DAO.OleDB
{
    /// <summary>
    /// 包间数据访问对象
    /// </summary>
    public class RoomDAO : IRoomDAO
    {
        #region Public functions
        public int Create(RS_Room room)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql =
                    "INSERT INTO RS_Room " +
                    "       (RoomName           " +
                    "       ,RoomType)          " +
                    "   VALUES                  " +
                    "       (@RoomName          " +
                    "       ,@RoomType)         " +
                    "SELECT @@IDENTITY          ";

                return Convert.ToInt32(opt.ExecuteScalar(
                    sql,
                    DBUtil.CreateSqlParameter(room.RoomName, "RoomName"),
                    DBUtil.CreateSqlParameter(room.RoomType, "RoomType")));
            }
        }

        public RS_Room Read(int roomId)
        {
            return Read(roomId, false);
        }

        public RS_Room Read(int roomId, bool isGreedLoad)
        {
            using (DBHelper opt = new DBHelper())
            {
                object room = null;
                string sql =
                    "SELECT                 " +
                    "    *                  " +
                    "FROM                   " +
                    "    RS_Room     " +
                    "WHERE                  " +
                    "    RoomId = @RoomId   ";

                SqlDataReader reader = opt.ExecuteQuery(
                    sql,
                    DBUtil.CreateSqlParameter(roomId, "RoomId"));

                if (reader.Read())
                {
                    room = new RS_Room();

                    DBUtil.ReadData(reader, ref room);

                    if (isGreedLoad)
                    {
                        ITableDAO tableDAO = new TableDAO();
                        (room as RS_Room).Tables = tableDAO.ReadAll((room as RS_Room).RoomId);
                    }
                }

                return room as RS_Room;
            }
        }

        public List<RS_Room> ReadAll(bool isGreedLoad)
        {
            using (DBHelper opt = new DBHelper())
            {
                List<RS_Room> rooms = new List<RS_Room>();
                string sql =
                    "SELECT *               " +
                    "   FROM RS_Room ";

                SqlDataReader readerRooms = opt.ExecuteQuery(sql);

                while (readerRooms.Read())
                {
                    object room = new RS_Room();
                    DBUtil.ReadData(readerRooms, ref room);

                    if (isGreedLoad)
                    {
                        ITableDAO tableDAO = new TableDAO();
                        (room as RS_Room).Tables = tableDAO.ReadAll((room as RS_Room).RoomId);
                    }

                    rooms.Add(room as RS_Room);
                }

                return rooms;
            }
        }

        public int Update(EntityDelta delta)
        {
            using (DBHelper opt = new DBHelper())
            {
                return opt.ExecuteNonQuery(
                    DBUtil.CreateUpdateSql(delta),
                    DBUtil.CreateUpdateParameters(delta));
            }
        }

        public int Delete(int roomId)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql =
                    "DELETE FROM RS_Room " +
                    "   WHERE RoomId = @RoomId  ";

                return opt.ExecuteNonQuery(
                    sql,
                    DBUtil.CreateSqlParameter(roomId, "RoomId"));
            }
        }
        #endregion
    }
}
